package com.parking.bang.rpc.core.server;

import java.util.Map;

import com.parking.bang.rpc.serialize.base.RPCSerializeProtocol;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

/**
 * Set Channel protocol & serialize
 * 
 * @author ivan
 *
 */
public class RPCServerChannelInitializer extends ChannelInitializer<SocketChannel> {
	private RPCSerializeProtocol rpcSerializeProtocol;
	private RPCServerSerializeSelector serializer = null;

	RPCServerChannelInitializer(Map<String, Object> handlerMap) {
		serializer = new RPCServerSerializeSelector(handlerMap);
	}

	RPCServerChannelInitializer setProtocol(RPCSerializeProtocol rpcSerializeProtocol) {
		this.rpcSerializeProtocol = rpcSerializeProtocol;
		return this;
	}

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		serializer.select(rpcSerializeProtocol, pipeline);
	}

}
